// +----------------------------------------------------------------------
// | CmsWing [ 网站内容管理框架 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2115 http://www.cmswing.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: arterli <arterli@qq.com>
// +----------------------------------------------------------------------
module.exports = class extends think.cmswing.center {
  constructor(ctx) {
    super(ctx);
  }
  async __before() {
    await super.__before();
    // 判断是否登陆
    // await this.weblogin();
    if (!this.is_login) {
      // 判断浏览客户端
      if (this.isMobile) {
        // 手机端直接跳转到登录页面
        return this.redirect('/center/public/login');
      } else {
        return this.redirect('/cmswing/error/login');
      }
    }
    if (!this.user.user_active) {
      const error = this.controller('cmswing/error');
      return error.noAction('用户未通过审核，禁止访问');
    } else {
      if (!this.user.user_pk) {
        const user_code = this.user.user_code;
        const params = {};
        if (this.user.groupid == 9) {
          params.sql = `select pk_customer user_pk from bd_customer where taxpayerid='${user_code}'`;
        } else if (this.user.groupid == 10) {
          params.sql = `select pk_supplier user_pk from bd_supplier where taxpayerid='${user_code}'`;
        }
        const resp = await this.requestNC('executeQuery', params);
        if (resp && resp.errno == 0) {
          this.user.user_pk = resp.data && resp.data.list[0] && resp.data.list[0].user_pk;
        }
      }
    }
  }

  /**
   * 销售合同
   */
  async contractAction() {
    this.meta_title = '销售合同';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

  /**
   * 订单明细
   */
  async orderAction() {
    this.meta_title = '订单明细';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

  /**
   * 公司余额
   */
  async companyAction() {
    this.meta_title = '公司余额';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

  /**
   * 发货/收货明细表头
   */
  async settlementAction() {
    this.meta_title = '发货明细';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      if (this.user.groupid == 9) {//客户
          this.meta_title = '发货明细';
      } else if (this.user.groupid == 10) {//供应商
          this.meta_title = '收货明细';
      }

      return this.display();
    }
  }

  /**
   * 客户发票明细
   */
  async invoiceAction() {
    this.meta_title = '发票明细';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

  /**
   * 供应商发票明细
   */
  async billingAction() {
    this.meta_title = '发票明细';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      return this.display();
    }
  }

  /**
   * 客户收款/供应商付款明细
   */
  async paymentAction() {
    this.meta_title = '付款明细';
    // 判断浏览客户端
    if (this.isMobile) {
      this.active = 'user/index';
      return this.display(this.mtpl());
    } else {
      if (this.user.groupid == 9) {//客户
        this.meta_title = '收款明细';
      } else if (this.user.groupid == 10) {//供应商
        this.meta_title = '付款明细';
      }
      return this.display();
    }
  }
};
